﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmChinh : Form
    {
        
        public frmChinh()
        {
            InitializeComponent();
            superTabControl1.SelectedTab = tabThuVienSach;
            cboTimSach.SelectedIndex = 0;
            frmQuyDinh.laygiatriquydinh();

        }
        public static bool check;

        private void btnLamTheThuVien_Click(object sender, EventArgs e)
        {
            superTabControl1.SelectedTab = tabQuanLyThanhVien;
        }

        private void btnLichSuTimSach_Click(object sender, EventArgs e)
        {
            frmLichSuTimSach frmlsts = new frmLichSuTimSach();
            frmlsts.ShowDialog();
        }

       

        private void btnThongTinCaNhan_Click(object sender, EventArgs e)
        {
            frmThongTinThanhVien tttv = new frmThongTinThanhVien();
            tttv.ShowDialog();
        }

        

        private void btnSachDaMuon_Click(object sender, EventArgs e)
        {
            frmSachDaMuon sdm = new frmSachDaMuon();
            sdm.ShowDialog();
        }

        private void btnQuanLyThanhVien_Click(object sender, EventArgs e)
        {
            superTabControl1.SelectedTab = tabQuanLyThanhVien;

        }

        private void btnQuyDinh_Click(object sender, EventArgs e)
        {
            frmQuyDinh qd = new frmQuyDinh();
            qd.ShowDialog();
        }

        private void btnMuon_Click(object sender, EventArgs e)
        {
            if (dgvMuonSach.RowCount == 0)
            {
                MessageBox.Show("Bạn Chưa Chọn Sách Để Mượn");
            }
            else
            {

                frmMuonSach pms = new frmMuonSach();
                pms.ShowDialog();
                dgvMuonSach.DataSource = null;
                dgvMuonSach.Refresh();
                dgvTimSach.DataSource = scontrol.Laysachvandausach();
            }
        
        }

        private void btnTraSach_Click(object sender, EventArgs e)
        {
            frmPhieuTraSach pts = new frmPhieuTraSach();
            pts.ShowDialog();
        }
        DausachControl dscontrol = new DausachControl();
        DataTable dttable = new DataTable();
        DocgiaControl control = new DocgiaControl();
        LoaitheControl ltcontrol = new LoaitheControl();
        CT_PTScontrol tsctrl = new CT_PTScontrol();
        ctpnsControl ctpnsctrl = new ctpnsControl();
        PTScontrol ptsctrl = new PTScontrol();
        ArrayList dsloaithe;
        SachControl scontrol = new SachControl();
        ConhieuControl cncontrol = new ConhieuControl();
        PhanquyenControl pqcontrol = new PhanquyenControl();
        bool kt = false;
        bool bInserted = false;
        public static string hihi;
        public static string madg2;
        public static int sotienno;
        public string[] madausach;
        string mds1, mds2, mds3, mds4, mds5, mds6, mds7, mds8, mds9,mds10,mds11,mds12;
        public static string mds;
         
        private void LaythongtinDocgia(ref DocgiaInfo info)
        {
            info.Madocgia = txtMaDocGia.Text;
            info.Gioitinh = cboGioiTinh.SelectedItem.ToString();

            info.Hoten = txtTenDocGia.Text;
            info.Cmnd = txtSoCMND.Text;
            info.Ngsinh = dtpNamSinh.Value;
            info.Nghenghiep = txtNgheNghiep.Text;
            info.Noilamviec = txtNoiLamViec.Text;
            info.Nglamthe = dtpNgayLamThe.Value;

            info.Nghethan = dtpNgayHetHan.Value;
            info.Diachi = txtDiaChi.Text;
            info.Dienthoai = txtSoDienThoai.Text;
            info.Sotienno = txtSoTienNo.Text;
            info.Trangthai = cboTrangThai.SelectedItem.ToString();
            info.Maloaithe = cboLoaiThe.SelectedValue.ToString();
            info.Mataikhoan = txtTenTaiKhoan.Text;
        }

        private bool kttuoitoithieutoida()
        {
            int ts;
            
            ts = Convert.ToInt16( DateTime.Today.Year) - Convert.ToInt16(  dtpNamSinh.Value.Year);
            if (ts < frmQuyDinh.tuoitoithieu || ts > frmQuyDinh.tuoitoida)
            {
                //MessageBox.Show("Tuoi phai tu " + frmQuyDinh.tuoitoithieu + " toi " + frmQuyDinh.tuoitoida + "");
                //dtpNamSinh.Focus();
                return false;
            }
            return true;
        }

        private void laythongtinphanquyen(ref PhanquyenInfo info)
        {
            info.Mataikhoan = txtTenTaiKhoan.Text;
            info.Matkhau = txtMatKhau.Text;
        }

        private PictureBox[] picarr;
        private PictureBox[] arrtop4muon;
        private PictureBox[] arrtop4vote;


        private void frmChinh_Load(object sender, EventArgs e)
        {
            
            SachControl scontrol = new SachControl();
            dgvTimSach.DataSource = scontrol.Laysachvandausach();
            madg2 = txtMaDocGia.Text;
            tabThuVienSach.Focus();
           datatabledt(dtSach);
           datatabledt(lstimsach);
           lbl_ten.Text = FrmDangNhap.txt1;
           if (FrmDangNhap.quyen == 0)
           {
               tabQuanLyThanhVien.Visible = false;
               rbbtab_quanly.Visible = false;
               btnMuon.Visible = false;
               TraSach.Visible = false;
               Lamthethuvien1.Visible = false;
               Thanhtoan.Visible = false;
               thongtinnhanvien.Visible = false;
           }
           picarr = new PictureBox[] { pic1, pic2, pic3, pic4 };
           madausach = new string[] { mds1, mds2, mds3, mds4, mds5, mds6, mds7, mds8,mds9,mds10,mds11,mds12 };
           DataTable dt = new DataTable();
            DataRow dr ;
           dt =  ctpnsctrl.top4sachmoi();
           for (int i = 0; i < 4; i++)
           {
               dr = dt.Rows[i];
               madausach[i] = dr["MADAUSACH"].ToString();
               if (dr.IsNull("HINHANH") == true)
                   continue;
               byte[] b = (byte[])dr["HINHANH"];
               MemoryStream st = new MemoryStream();
               st.Write(b, 0, b.Length);
               Image im = Image.FromStream(st);
               picarr[i].Image = im;
           }

           arrtop4muon = new PictureBox[] { pic5, pic6, pic7, pic8 };
           DataTable dt2 = new DataTable();
           DataRow dr2;
           dt2 = dscontrol.top4sachmuon() ;

            

           for (int i = 0; i < 4; i++)
           {
               
               dr2 = dt2.Rows[i];
               madausach[4 + i] = dr2["MADAUSACH"].ToString();
               if (dr2.IsNull("HINHANH") == true)
                   continue;
               byte[] b = (byte[])dr2["HINHANH"];
               MemoryStream st = new MemoryStream();
               st.Write(b, 0, b.Length);
               Image im = Image.FromStream(st);
               arrtop4muon[i].Image = im;
           }

           arrtop4vote = new PictureBox[] { pic9, pic10, pic11, pic12 };
           DataTable dt3 = new DataTable();
           DataRow dr3;
           dt3 = dscontrol.top4vote();
           for (int i = 0; i < 4; i++)
           {

               dr3 = dt3.Rows[i];
               madausach[8 + i] = dr3["MADAUSACH"].ToString();
               if (dr3.IsNull("HINHANH") == true)
                   continue;
               byte[] b = (byte[])dr3["HINHANH"];
               MemoryStream st = new MemoryStream();
               st.Write(b, 0, b.Length);
               Image im = Image.FromStream(st);
               arrtop4vote[i].Image = im;
           }
           
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DocgiaInfo info = new DocgiaInfo();
            PhanquyenInfo pqinfo = new PhanquyenInfo();
            if (
                    txtNgheNghiep.Text == "" ||
                    txtNoiLamViec.Text == "" ||
                    txtSoCMND.Text == "" ||
                    txtDiaChi.Text == "" ||
                    txtSoDienThoai.Text == "" ||
                    txtSoTienNo.Text == "" ||
                    txtTenDocGia.Text == "" ||
                    txtTenTaiKhoan.Text == "" ||
                    txtMatKhau.Text == ""
                    )
            {
                MessageBox.Show("Chưa Nhập Đầy Đủ Các Thành Phần Yêu Cầu !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (bInserted)
                {
                    LaythongtinDocgia(ref info);
                    laythongtinphanquyen(ref pqinfo);
                    if (kiemtra(txtMaDocGia.Text.Trim()) == true)
                    {
                        MessageBox.Show("Trùng Mã Độc Giả");
                    }
                    else
                    {
                        if (kttuoitoithieutoida() == false)
                        {
                            MessageBox.Show("Tuoi phai tu " + frmQuyDinh.tuoitoithieu + " toi " + frmQuyDinh.tuoitoida + "");
                            dtpNamSinh.Focus();
                        }
                        else
                        {
                            pqcontrol.insertphanquyenDG(pqinfo);
                            control.insert(info);
                            MessageBox.Show("Thêm Dữ Liệu Thành Công");
                            dgvThongTinDocGia.DataSource = control.LayDSdocgia();
                            bInserted = false;
                            ShowControl(false);
                            btnThemS.Enabled = true;
                            btnSuaS.Enabled = true;
                        }
                    }
                }

                else
                {
                    if (kttuoitoithieutoida() == false)
                    {
                        MessageBox.Show("Tuoi phai tu " + frmQuyDinh.tuoitoithieu + " toi " + frmQuyDinh.tuoitoida + "");
                        dtpNamSinh.Focus();
                    }
                    else
                    {
                        laythongtinphanquyen(ref pqinfo);
                        LaythongtinDocgia(ref info);
                        pqcontrol.updatephanquyen(pqinfo);
                        control.update(info);
                        MessageBox.Show("Sửa Thành Công");
                        tabQuanLyThanhVien_Click(sender, e);
                        ShowControl(false);
                        btnThemS.Enabled = true;
                        btnSuaS.Enabled = true;
                    }
                }
            }
        }

        private void dgvThongTinDocGia_SelectionChanged(object sender, EventArgs e)
        {

            if (dgvThongTinDocGia.RowCount > 0)
            {
                txtMaDocGia.Text = dgvThongTinDocGia.CurrentRow.Cells["MADG"].Value.ToString();
                cboLoaiThe.SelectedValue = dgvThongTinDocGia.CurrentRow.Cells["MALOAITHE"].Value.ToString();
                txtTenDocGia.Text = dgvThongTinDocGia.CurrentRow.Cells["HOTEN"].Value.ToString();
                txtSoCMND.Text = dgvThongTinDocGia.CurrentRow.Cells["CMND"].Value.ToString();
                dtpNamSinh.Text = dgvThongTinDocGia.CurrentRow.Cells["NGSINH"].Value.ToString() ;
                dtpNgayLamThe.Text = dgvThongTinDocGia.CurrentRow.Cells["NGLAMTHE"].Value.ToString();
                dtpNgayHetHan.Text = dgvThongTinDocGia.CurrentRow.Cells["NGHETHAN"].Value.ToString();
                txtNgheNghiep.Text = dgvThongTinDocGia.CurrentRow.Cells["NGHENGHIEP"].Value.ToString();
                cboGioiTinh.SelectedItem = dgvThongTinDocGia.CurrentRow.Cells["GIOITINH"].Value.ToString();
                cboTrangThai.SelectedItem = dgvThongTinDocGia.CurrentRow.Cells["TRANGTHAI"].Value.ToString();
                txtMatKhau.Text = dgvThongTinDocGia.CurrentRow.Cells["MATKHAU"].Value.ToString();
                txtTenTaiKhoan.Text = dgvThongTinDocGia.CurrentRow.Cells["MATAIKHOAN"].Value.ToString();
                txtDiaChi.Text = dgvThongTinDocGia.CurrentRow.Cells["DIACHI"].Value.ToString();
                txtNoiLamViec.Text = dgvThongTinDocGia.CurrentRow.Cells["NOILAMVIEC"].Value.ToString();
                txtSoTienNo.Text = dgvThongTinDocGia.CurrentRow.Cells["SOTIENNO"].Value.ToString();
                txtSoDienThoai.Text = dgvThongTinDocGia.CurrentRow.Cells["DIENTHOAI"].Value.ToString();
            }
        }

        private void btnSuaS_Click(object sender, EventArgs e)
        {
            bInserted = false;
            ShowControl(true);
            btnThemS.Enabled = false;
            btnSuaS.Enabled = false;
        }

        private void btnNhapSach_Click(object sender, EventArgs e)
        {
            frmNhapSach nhapsach = new frmNhapSach();
            nhapsach.ShowDialog();
        }


        private void tabQuanLyThanhVien_Click(object sender, EventArgs e)
        {
            dsloaithe = ltcontrol.getDSloaithe();
            cboLoaiThe.DisplayMember = "TENLOAITHE";
            cboLoaiThe.ValueMember = "MALOAITHE";
            cboLoaiThe.DataSource = dsloaithe;
            cboTrangThai.SelectedIndex = 0;
            cboLoaiThe.SelectedIndex = 0;
            cboGioiTinh.SelectedIndex = 0;
            DocgiaControl docgiacontrol = new DocgiaControl();
            dgvThongTinDocGia.DataSource = docgiacontrol.LayDSdocgia();
        }

        public void datatabledt(DataTable dt)
        {
            
            dt.Columns.Add("MADAUSACH");
            dt.Columns.Add("MATHELOAI");
            dt.Columns.Add("TENSACH");
            dt.Columns.Add("NXB");
            dt.Columns.Add("NAMXB");
            dt.Columns.Add("TRIGIA");
            dt.Columns.Add("MASACH");
            dt.Columns.Add("TINHTRANG");
        }
        public static DataTable dtSach = new DataTable();
        private void btn_them_Click(object sender, EventArgs e)
        {
            
            if (dgvMuonSach.DataSource == null || dtSach.Rows.Count ==0)
            {
                dtSach.Clear();
                string[] dr = { dgvTimSach.CurrentRow.Cells[0].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[1].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[2].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[3].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[4].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[5].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[6].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[7].Value.ToString()};
                dtSach.Rows.Add(dr);
                dgvMuonSach.DataSource = dtSach;
            }
            else
            {
                //dtSach.Clear();
                bool kiemtra = false;
                for (int i = 0; i <= dgvMuonSach.Rows.Count-1; i++)
                {
                    if (dgvTimSach.CurrentRow.Cells[0].Value.ToString() == dgvMuonSach.Rows[i].Cells[0].Value.ToString())
                    
                    { kiemtra = false; break; }
                    else
                    {
                        kiemtra = true;


                    }
                   
                }

                if (kiemtra)
                {
                    string[] dr = { dgvTimSach.CurrentRow.Cells[0].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[1].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[2].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[3].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[4].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[5].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[6].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[7].Value.ToString()};
                    dtSach.Rows.Add(dr);
                    dgvMuonSach.DataSource = dtSach;
                }
                else
                {
                    MessageBox.Show("Sách Đã Được Thêm");
                }
            }
            
        }
        public DataTable muonsach()
        {
            DataTable dt = new DataTable();
            datatabledt(dt); 
            for (int i = 0; 0 < dgvMuonSach.Rows.Count; i++)
            {
                string[] dr = { dgvMuonSach.Rows[i].Cells[0].Value.ToString(),
                              dgvMuonSach.Rows[i].Cells[1].Value.ToString(),
                              dgvMuonSach.Rows[i].Cells[2].Value.ToString(),
                              dgvMuonSach.Rows[i].Cells[3].Value.ToString(),
                              dgvMuonSach.Rows[i].Cells[4].Value.ToString(),
                              dgvMuonSach.Rows[i].Cells[5].Value.ToString(),
                              dgvMuonSach.Rows[i].Cells[6].Value.ToString(),
                              dgvMuonSach.Rows[i].Cells[7].Value.ToString()};
                dt.Rows.Add(dr);
            }          
            return dt;
        }

        private void btnXoaSM_Click(object sender, EventArgs e)
        {
            if (dgvMuonSach.RowCount != 0)
            {
                dgvMuonSach.Rows.RemoveAt(dgvMuonSach.CurrentRow.Index);
            }
            else
            {
                dtSach.Clear();
            }

        }
                  
        private void tabThuVienSach_Click(object sender, EventArgs e)
        {
            tabThuVienSach.Visible = true;
        }

        public static DataTable lstimsach = new DataTable();

        private void btnTimSach_Click(object sender, EventArgs e)
        {
            
            superTabControl1.SelectedTab = tabTimSach;
            hihi = txtTimSach.Text.Trim();
            
            if (cboTimSach.SelectedItem == "Tên sách")
            {
                SachControl scontrol = new SachControl();
                dgvTimSach.DataSource = scontrol.timsach();
            }
            if (cboTimSach.SelectedItem == "Tác giả")
            {
                SachControl scontrol = new SachControl();
                dgvTimSach.DataSource = scontrol.sachtheotacgia();
            }
            if(dgvTimSach.RowCount == 0)
                {
 
                    MessageBox.Show("Sách không tìm thấy");
                }
            else
                {
            
            string[] dr = { dgvTimSach.CurrentRow.Cells[0].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[1].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[2].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[3].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[4].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[5].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[6].Value.ToString(),
                          dgvTimSach.CurrentRow.Cells[7].Value.ToString()};
            lstimsach.Rows.Add(dr);
                }
        }

        private void btn_tacgia_Click(object sender, EventArgs e)
        {
            frmLoaiTacGia ltg = new frmLoaiTacGia();
            ltg.ShowDialog();
        }

        private void laythongtinconhieu(ref ConhieuInfo info)
        {
            info.Madausach = dgvTimSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
            
        }

        private void hien()
        {
            lbl_masach.Visible = true;
            lbl_namxuatban.Visible = true;
            lbl_nhaxuatban.Visible = true;
            lbl_tacgia.Visible = true;
            lbl_tensach.Visible = true;
            lbl_theloai.Visible = true;
            lbl_tinhtrang.Visible = true;
            lbl_trigia.Visible = true;
        }


        private void dgv_timsach_SelectionChanged(object sender, EventArgs e)
        {
            hien();
            ConhieuInfo info = new ConhieuInfo();
            laythongtinconhieu(ref info);
            cncontrol.LayTacgia(info);
            DataTable dt = new DataTable();
            dt = cncontrol.LayTacgia(info);
            string temp = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                temp = temp + dt.Rows[i]["TENTG"].ToString();
                temp = temp + " , ";
            }

            if (dgvTimSach.RowCount > 0)
            {
                lbl_masach.Text = dgvTimSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
                lbl_namxuatban.Text = dgvTimSach.CurrentRow.Cells["NAMXB"].Value.ToString();
                lbl_nhaxuatban.Text = dgvTimSach.CurrentRow.Cells["NXB"].Value.ToString();
                lbl_tacgia.Text = temp;
                lbl_tensach.Text = dgvTimSach.CurrentRow.Cells["TENSACH"].Value.ToString();
                lbl_theloai.Text = dgvTimSach.CurrentRow.Cells["MATHELOAI"].Value.ToString();
                lbl_tinhtrang.Text = dgvTimSach.CurrentRow.Cells["TINHTRANG"].Value.ToString();
                lbl_trigia.Text = dgvTimSach.CurrentRow.Cells["TRIGIA"].Value.ToString();
                byte[] b = (byte[])dgvTimSach.CurrentRow.Cells["HINHANH"].Value;
                MemoryStream st = new MemoryStream();
                st.Write(b, 0, b.Length);
                Image im = Image.FromStream(st);
                picSach.Image = im;
            }
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            frmNhanVien fnv = new frmNhanVien();
            fnv.Show();
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            frmNhapKho frmkho = new frmNhapKho();
            frmkho.Show();
        }

       

        private Boolean kiemtra(string ma)
        {

            for (int j = 0; j < dgvThongTinDocGia.RowCount ; j++)
            {
                if (ma == dgvThongTinDocGia.Rows[j].Cells[0].Value.ToString())
                {
                    kt = true;
                    break;
                }
                else
                    kt = false;
            }
            return kt;
        }
        private void ShowControl(bool bShow)
        {
            //txtMaDocGia.Enabled = bInserted;
            txtNgheNghiep.Enabled = bShow;
            txtNoiLamViec.Enabled = bShow;
            txtSoCMND.Enabled = bShow;
            txtDiaChi.Enabled = bShow;
            txtSoDienThoai.Enabled = bShow;
            txtSoTienNo.Enabled = bShow;
            txtTenDocGia.Enabled = bShow;
            txtTenTaiKhoan.Enabled = bInserted;
            txtMatKhau.Enabled = bShow;
            cboGioiTinh.Enabled = bShow;
            cboLoaiThe.Enabled = bShow;
            cboTrangThai.Enabled = bShow;
            dtpNamSinh.Enabled = bShow;
            dtpNgayHetHan.Enabled = bShow;
            dtpNgayLamThe.Enabled = bShow;
            btnOk.Enabled = bShow;
        }
        private void Clear()
        {
            txtMaDocGia.Text = "";
            txtNgheNghiep.Text = "";
            txtNoiLamViec.Text = "";
            txtSoCMND.Text = "";
            txtDiaChi.Text = "";
            txtSoDienThoai.Text = "";
            txtSoTienNo.Text = "";
            txtTenDocGia.Text = "";
            txtTenTaiKhoan.Text = "";
            txtMatKhau.Text = "";

        }

        private void btnThemS_Click(object sender, EventArgs e)
        {

            bInserted = true;
            Clear();
            ShowControl(true);
            btnThemS.Enabled = false;
            btnSuaS.Enabled = false;
            int i = dgvThongTinDocGia.Rows.Count + 1;
            txtMaDocGia.Text = "DG" + i + "";
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {

            if (dgvThongTinDocGia.RowCount > 0)
            {
                txtMaDocGia.Text = dgvThongTinDocGia.CurrentRow.Cells["MADG"].Value.ToString();
                cboLoaiThe.SelectedValue = dgvThongTinDocGia.CurrentRow.Cells["MALOAITHE"].Value.ToString();
                txtTenDocGia.Text = dgvThongTinDocGia.CurrentRow.Cells["HOTEN"].Value.ToString();
                txtSoCMND.Text = dgvThongTinDocGia.CurrentRow.Cells["CMND"].Value.ToString();
                dtpNamSinh.Text = dgvThongTinDocGia.CurrentRow.Cells["NGSINH"].Value.ToString();
                dtpNgayLamThe.Text = dgvThongTinDocGia.CurrentRow.Cells["NGLAMTHE"].Value.ToString();
                dtpNgayHetHan.Text = dgvThongTinDocGia.CurrentRow.Cells["NGHETHAN"].Value.ToString();
                txtNgheNghiep.Text = dgvThongTinDocGia.CurrentRow.Cells["NGHENGHIEP"].Value.ToString();
                cboGioiTinh.SelectedItem = dgvThongTinDocGia.CurrentRow.Cells["GIOITINH"].Value.ToString();
                cboTrangThai.SelectedItem = dgvThongTinDocGia.CurrentRow.Cells["TRANGTHAI"].Value.ToString();
                txtMatKhau.Text = dgvThongTinDocGia.CurrentRow.Cells["MATKHAU"].Value.ToString();
                txtTenTaiKhoan.Text = dgvThongTinDocGia.CurrentRow.Cells["MATAIKHOAN"].Value.ToString();
                txtDiaChi.Text = dgvThongTinDocGia.CurrentRow.Cells["DIACHI"].Value.ToString();
                txtNoiLamViec.Text = dgvThongTinDocGia.CurrentRow.Cells["NOILAMVIEC"].Value.ToString();
                txtSoTienNo.Text = dgvThongTinDocGia.CurrentRow.Cells["SOTIENNO"].Value.ToString();
                txtSoDienThoai.Text = dgvThongTinDocGia.CurrentRow.Cells["DIENTHOAI"].Value.ToString();
            }
            bInserted = false;
            ShowControl(false);
            btnThemS.Enabled = true;
            btnSuaS.Enabled = true;

        }
        private void KiemTraNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Yêu Cầu Nhập Số");
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();        
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void frmChinh_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult Thoat = MessageBox.Show("Bạn Có Chắc Muốn Thoát Không ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop);
            if (Thoat == DialogResult.Cancel)
            {
                e.Cancel = true;
            }           
        }

        private void btnThongTinThanhVien_Click(object sender, EventArgs e)
        {
            frmThongTinThanhVien frmtttv = new frmThongTinThanhVien();
            frmtttv.ShowDialog();
        }

        private void pic1_Click(object sender, EventArgs e)
        {
            mds = madausach[0];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
            
        }

        private void pic2_Click(object sender, EventArgs e)
        {
            mds = madausach[1];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic3_Click(object sender, EventArgs e)
        {
            mds = madausach[2];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic4_Click(object sender, EventArgs e)
        {
            mds = madausach[3];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic5_Click(object sender, EventArgs e)
        {
            mds = madausach[4];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic6_Click(object sender, EventArgs e)
        {
            mds = madausach[5];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic7_Click(object sender, EventArgs e)
        {
            mds = madausach[6];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic8_Click(object sender, EventArgs e)
        {
            mds = madausach[7];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        public static string timsach;
        private void txtTimSach_TextChanged(object sender, EventArgs e)
        {
            timsach = txtTimSach.Text;                   
        }

        private void btnThongTinNhanVien_Click(object sender, EventArgs e)
        {
            frmThongTinNhanVien frmttnv = new frmThongTinNhanVien();
            frmttnv.ShowDialog();
        }

        public static string timdg;
        private void txtTimNhanVien_TextChanged(object sender, EventArgs e)
        {
            timdg = txtTimNhanVien.Text;
            dgvThongTinDocGia.DataSource = control.timdg();
        }

        private void pic9_Click(object sender, EventArgs e)
        {
            mds = madausach[8];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic10_Click(object sender, EventArgs e)
        {
            mds = madausach[9];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic11_Click(object sender, EventArgs e)
        {
            mds = madausach[10];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void pic12_Click(object sender, EventArgs e)
        {
            mds = madausach[11];
            frmThongTinSach TTS = new frmThongTinSach();
            TTS.Show();
        }

        private void btngiahan_ItemClick(object sender, EventArgs e)
        {
            frmGiaHan frmgh = new frmGiaHan();
            frmgh.Show();
        }

        private void buttonItem5_Click(object sender, EventArgs e)
        {
            
            frmChinh.check = false;
            frmTraTien vp = new frmTraTien();
            vp.Show();
        }

        private void dtpNgayLamThe_ValueChanged(object sender, EventArgs e)
        {
            //TimeSpan ts;
            //ts = DateTime.Today + 
            //dtpNgayHetHan.Value.Add(


        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            superTabControl1.SelectedTab = tabQuanLyThanhVien;
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            frmThongKe frm = new frmThongKe();
            frm.ShowDialog();
        }
      
    }
}
